// Ch4_8.cpp : Initialization functions

// CH4_8.CPP
// by Charles Mc Auley
// "Programming AutoCAD 2000 with ObjectARX"
//
// This application demonstrate how to iterate
// through all the attributes of a selected block
// reference and prompts the user to change
// the value of the selected Attribute. In order
// to use the application, you must have a block
// entity already present in the drawing and that
// block reference entity must contains attributes.
//
/////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "StdArx.h"
#include "resource.h"

HINSTANCE _hdllInstance =NULL ;

// This command registers an ARX command.
void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
				const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal = -1);

// NOTE: DO NOT edit the following lines.
//{{AFX_ARX_MSG
void InitApplication();
void UnloadApplication();
//}}AFX_ARX_MSG

// NOTE: DO NOT edit the following lines.
//{{AFX_ARX_ADDIN_FUNCS
//}}AFX_ARX_ADDIN_FUNCS

/////////////////////////////////////////////////////////////////////////////
// DLL Entry Point
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
	if (dwReason == DLL_PROCESS_ATTACH)
	{
        _hdllInstance = hInstance;
	} else if (dwReason == DLL_PROCESS_DETACH) {
	}
	return TRUE;    // ok
}



/////////////////////////////////////////////////////////////////////////////
// ObjectARX EntryPoint
extern "C" AcRx::AppRetCode 
acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
{
	switch (msg) {
	case AcRx::kInitAppMsg:
		// Comment out the following line if your
		// application should be locked into memory
		acrxDynamicLinker->unlockApplication(pkt);
		acrxDynamicLinker->registerAppMDIAware(pkt);
		InitApplication();
		break;
	case AcRx::kUnloadAppMsg:
		UnloadApplication();
		break;
	}
	return AcRx::kRetOK;
}


// Init this application. Register your
// commands, reactors...
void InitApplication()
{
	// NOTE: DO NOT edit the following lines.
	//{{AFX_ARX_INIT
	AddCommand("CH4_APPS", "PKBLK", "PKBLK", ACRX_CMD_MODAL, pkblk);
	//}}AFX_ARX_INIT

	// TODO: add your initialization functions

}

// Unload this application. Unregister all objects
// registered in InitApplication.
void UnloadApplication()
{
	// NOTE: DO NOT edit the following lines.
	//{{AFX_ARX_EXIT
	acedRegCmds->removeGroup("CH4_APPS");
	//}}AFX_ARX_EXIT

	// TODO: clean up your application
}

// This functions registers an ARX command.
// It can be used to read the localized command name
// from a string table stored in the resources.
void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
				const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal)
{
	char cmdLocRes[65];

	// If idLocal is not -1, it's treated as an ID for
	// a string stored in the resources.
	if (idLocal != -1) {

		// Load strings from the string table and register the command.
		::LoadString(_hdllInstance, idLocal, cmdLocRes, 64);
		acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLocRes, cmdFlags, cmdProc);

	} else
		// idLocal is -1, so the 'hard coded'
		// localized function name is used.
		acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLoc, cmdFlags, cmdProc);
}

// User defined function called by
// pkblk() see Ch4_8Commands.cpp
AcDbAttributeDefinition* getAttDef(char* tagValue, AcDbBlockTableRecord* pBlkDefRecord)
{
	AcDbAttributeDefinition *pRetAttDef = NULL;
	AcDbBlockTableRecordIterator *pAttDefItr;
	AcDbEntity *pEnt;
	char thisTag[255];
	char* pThisTag;

	pBlkDefRecord->newIterator(pAttDefItr);
	for(pAttDefItr->start(); !pAttDefItr->done(); pAttDefItr->step())
	{
		pAttDefItr->getEntity(pEnt, AcDb::kForRead);
		pRetAttDef = AcDbAttributeDefinition::cast(pEnt);
		if(pRetAttDef != NULL && !pRetAttDef->isConstant())
		{
			pThisTag = pRetAttDef->tag();
			strcpy(thisTag, pThisTag);
			free(pThisTag);
			if(strcmp(thisTag, tagValue) == 0)
			{
				delete pAttDefItr;
				return pRetAttDef;
			}
		}

		pEnt->close();

	}// for
	
	delete pAttDefItr;
	return pRetAttDef;
}

